package com.telecom.cms.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.telecom.cms.dto.IdaTemplate;
import com.telecom.cms.dto.IdaTemplateAtomParam;
import com.telecom.cms.dto.IdaTemplateAtomResult;
import com.telecom.cms.dto.IdaTemplateAtomSql;
import com.telecom.cms.forms.IdaTemplateForm;
import com.telecom.cms.service.IdaMgr;

public class IdaTemplateAction extends DispatchAction 
{
	private IdaMgr idamgr;
	
	private void getCommon(HttpServletRequest request){
		List templateList = idamgr.findAllTemplate();
		request.setAttribute("templateList", templateList);
		
		String templateId = request.getParameter("templateId");
		if (templateId == null && request.getAttribute("templateId") != null)
			templateId = (String)request.getAttribute("templateId");
		if (templateId != null && !templateId.equals("")){
			int tid = Integer.parseInt(templateId);
			for (int i = 0; i < templateList.size(); i++){
				IdaTemplate template = (IdaTemplate)templateList.get(i);
				if (template.getTemplateId() == tid){
					request.setAttribute("idaTemplate", template);
					if (request.getParameter("reloadParam") == null && template.getAtomList().size() > 0){
						if (template.getTemplateTypeId().equals("10"))
							request.getSession().setAttribute("paramList", template.getAtomList());
						if (template.getTemplateTypeId().equals("30"))
							request.getSession().setAttribute("sqlParamList", template.getAtomList());
					}
				}
			}
		}
		else{
			request.getSession().removeAttribute("paramList");
			request.getSession().removeAttribute("sqlParamList");
		}
	}
	
	public ActionForward paramTemplate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		getCommon(request);
		return mapping.findForward("paramTemplate");
	}
	
	public ActionForward resultTemplate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		getCommon(request);
		return mapping.findForward("resultTemplate");
	}
	
	public ActionForward sqlTemplate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		getCommon(request);
		return mapping.findForward("sqlTemplate");
	}
	
	public ActionForward insertTemplate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		IdaTemplateForm idaform = (IdaTemplateForm)form;
		IdaTemplate template = idaform.getIdaTemplate();
		int templateId = idamgr.insertTemplate(template);
		
		if (templateId > 0 && template.getTemplateTypeId().equals("10")){
			Object o = request.getSession().getAttribute("paramList");
			if (o != null){
				List paramList = (List)o;
				for (int i = 0; i < paramList.size(); i++){
					IdaTemplateAtomParam atom = (IdaTemplateAtomParam)paramList.get(i);
					atom.setTemplateId(templateId);
					idamgr.insertTemplateAtomParam(atom);
				}
				request.getSession().removeAttribute("paramList");
			}
			else {
				String[] atoms = request.getParameterValues("ObjSelect");
				if (atoms != null){
					for (int i = 0; i < atoms.length; i++){
						IdaTemplateAtomParam atom = idamgr.findTemplateAtomParamInit(Integer.parseInt(atoms[i]));
						atom.setTemplateId(templateId);
						atom.setDisplayId(i + 1);
						idamgr.insertTemplateAtomParam(atom);
					}
				}
			}
		}
		else if (templateId > 0 && template.getTemplateTypeId().equals("20")){
			String[] atoms = request.getParameterValues("ObjSelect");
			if (atoms != null){
				for (int i = 0; i < atoms.length; i++){
					String orderRule = "";
					if (atoms[i].indexOf("#") != -1){
						orderRule = atoms[i].substring(atoms[i].indexOf("#") + 1);
						atoms[i] = atoms[i].substring(0,atoms[i].indexOf("#"));
					}
					IdaTemplateAtomResult atom = idamgr.findTemplateAtomResultInit(Integer.parseInt(atoms[i]));
					atom.setTemplateId(templateId);
					atom.setResultOrderRule(orderRule);
					atom.setDisplayId(i + 1);
					idamgr.insertTemplateAtomResult(atom);
				}
			}
		}
		else if (templateId > 0 && template.getTemplateTypeId().equals("30")){
			Object o = request.getSession().getAttribute("sqlParamList");
			if (o != null){
				List sqlParamList = (List)o;
				for (int i = 0; i < sqlParamList.size(); i++){
					IdaTemplateAtomSql atom = (IdaTemplateAtomSql)sqlParamList.get(i);
					atom.setTemplateId(templateId);
					idamgr.insertTemplateAtomSql(atom);
				}
				request.getSession().removeAttribute("sqlParamList");
			}
		}
		else ;
		
		if (templateId > 0){
			template.setTemplateId(templateId);
			request.setAttribute("templateId", String.valueOf(templateId));
		}
		getCommon(request);
		if (template.getTemplateTypeId().equals("10"))
			return mapping.findForward("paramTemplate"); 
		else if (template.getTemplateTypeId().equals("20"))
			return mapping.findForward("resultTemplate"); 
		else if (template.getTemplateTypeId().equals("30"))
			return mapping.findForward("sqlTemplate"); 
		else
			return mapping.findForward("failure"); 
	} 
	
	public ActionForward updateTemplate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		IdaTemplateForm idaform = (IdaTemplateForm)form;
		IdaTemplate template = idaform.getIdaTemplate();
		idamgr.updateTemplate(template);
		request.setAttribute("templateId", String.valueOf(template.getTemplateId()));
		
		if (template.getTemplateTypeId().equals("10")){
			Object o = request.getSession().getAttribute("paramList");
			if (o != null){
				List paramList = (List)o;
				for (int i = 0; i < paramList.size(); i++){
					IdaTemplateAtomParam atom = (IdaTemplateAtomParam)paramList.get(i);
					atom.setTemplateId(template.getTemplateId());
					idamgr.insertTemplateAtomParam(atom);
				}
				request.getSession().removeAttribute("paramList");
			}
			else{
				String[] atoms = request.getParameterValues("ObjSelect");
				if (atoms != null){
					for (int i = 0; i < atoms.length; i++){
						IdaTemplateAtomParam atom = idamgr.findTemplateAtomParamInit(Integer.parseInt(atoms[i]));
						atom.setTemplateId(template.getTemplateId());
						atom.setDisplayId(i + 1);
						idamgr.insertTemplateAtomParam(atom);
					}
				}
			}
		}
		else if (template.getTemplateTypeId().equals("20")){
			String[] atoms = request.getParameterValues("ObjSelect");
			if (atoms != null){
				for (int i = 0; i < atoms.length; i++){
					String orderRule = "";
					if (atoms[i].indexOf("#") != -1){
						orderRule = atoms[i].substring(atoms[i].indexOf("#") + 1);
						atoms[i] = atoms[i].substring(0,atoms[i].indexOf("#"));
					}
					IdaTemplateAtomResult atom = idamgr.findTemplateAtomResultInit(Integer.parseInt(atoms[i]));
					atom.setTemplateId(template.getTemplateId());
					atom.setResultOrderRule(orderRule);
					atom.setDisplayId(i + 1);
					idamgr.insertTemplateAtomResult(atom);
				}
			}
		}
		else if (template.getTemplateTypeId().equals("30")){
			Object o = request.getSession().getAttribute("sqlParamList");
			if (o != null){
				List sqlParamList = (List)o;
				for (int i = 0; i < sqlParamList.size(); i++){
					IdaTemplateAtomSql atom = (IdaTemplateAtomSql)sqlParamList.get(i);
					atom.setTemplateId(template.getTemplateId());
					idamgr.insertTemplateAtomSql(atom);
				}
				request.getSession().removeAttribute("sqlParamList");
			}
		}
		else ;
		
		getCommon(request);
		if (template.getTemplateTypeId().equals("10"))
			return mapping.findForward("paramTemplate"); 
		else if (template.getTemplateTypeId().equals("20"))
			return mapping.findForward("resultTemplate"); 
		else if (template.getTemplateTypeId().equals("30"))
			return mapping.findForward("sqlTemplate"); 
		else
			return mapping.findForward("failure"); 
	} 
	
	public ActionForward deleteTemplate(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		IdaTemplateForm idaform = (IdaTemplateForm)form;
		IdaTemplate template = idaform.getIdaTemplate();
		idamgr.deleteTemplate(template);
		getCommon(request);
		if (template.getTemplateTypeId().equals("10"))
			return mapping.findForward("paramTemplate"); 
		else if (template.getTemplateTypeId().equals("20"))
			return mapping.findForward("resultTemplate"); 
		else if (template.getTemplateTypeId().equals("30"))
			return mapping.findForward("sqlTemplate"); 
		else
			return mapping.findForward("failure"); 
	}
	
	public ActionForward buildParam(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		IdaTemplateForm idaform = (IdaTemplateForm)form;
		IdaTemplate template = idaform.getIdaTemplate();
		if (template.getTemplateId() > 0)
			request.setAttribute("idaTemplate", template);
		else
			request.getSession().setAttribute("curTemplate", template);
		getCommon(request);
		
		List paramList = new ArrayList();
		String[] atoms = request.getParameterValues("ObjSelect");
		if (atoms != null){
			for (int i = 0; i < atoms.length; i++){
				IdaTemplateAtomParam atom = idamgr.findTemplateAtomParamInit(Integer.parseInt(atoms[i]));
				atom.setTemplateId(template.getTemplateId());
				atom.setDisplayId(i + 1);
				atom.setIsNotEmpty("20");
				atom.setIsFiltOnly("20");
				atom.setIsLikeMatch("20");
				paramList.add(atom);
			}
		}
		if (paramList.size() > 0)
			request.getSession().setAttribute("paramList", paramList);
		else
			request.getSession().removeAttribute("paramList");
		
		return mapping.findForward("paramTemplate"); 
	}
	
	public ActionForward buildSqlParam(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		IdaTemplateForm idaform = (IdaTemplateForm)form;
		IdaTemplate template = idaform.getIdaTemplate();
		if (template.getTemplateId() > 0)
			request.setAttribute("idaTemplate", template);
		else
			request.getSession().setAttribute("curTemplate", template);
		getCommon(request);
		
		List sqlParamList = new ArrayList();
		String sql = template.getSqlStmt();
		int idx = 0;
		int tempidx = 0;
		int tempcnt = 0;
		while (sql.indexOf("{", idx) != -1){
			String paramName = sql.substring(sql.indexOf("{",idx),sql.indexOf("}",idx) + 1);
			String tempStr = sql.substring(tempidx, sql.indexOf("{",tempidx)).toUpperCase();
			if (tempStr.indexOf("WHERE") != -1)
				tempStr = tempStr.substring(tempStr.indexOf("WHERE") + 6).trim();
			else if (tempStr.indexOf("AND") != -1)
				tempStr = tempStr.substring(tempStr.indexOf("AND") + 4).trim();
			String colName = "";
			if (tempStr.indexOf("=") != -1)
				colName = tempStr.substring(0,tempStr.indexOf("=")).trim();
			else
				colName = tempStr.substring(0,tempStr.indexOf(" "));
			
			if (tempStr.indexOf("BETWEEN") != -1 && ++tempcnt < 2)
				tempidx = idx;
			else if (tempStr.indexOf(" IN") != -1 && tempStr.indexOf(")") == -1){
				String s = sql.substring(0,sql.indexOf(")",tempidx));
				int i1 = s.indexOf("}",idx);
				int i2 = s.lastIndexOf("}");
				if (i1 == i2)
					tempidx = sql.indexOf("}", idx) + 1;
			}
			else{
				tempidx = sql.indexOf("}", idx) + 1;
				tempcnt = 0;
			}
			idx = sql.indexOf("}", idx) + 1;
			
			IdaTemplateAtomSql atom = idamgr.findTemplateAtomSqlInit(colName);
			atom.setTemplateId(template.getTemplateId());
			atom.setParamName(paramName);
			atom.setDmnColumnName1(colName);
			atom.setDisplayId(sqlParamList.size() + 1);
			atom.setIsNotEmpty("20");
			atom.setIsFiltOnly("20");
			atom.setIsLikeMatch("20");
			sqlParamList.add(atom);
		}
		if (sqlParamList.size() > 0)
			request.getSession().setAttribute("sqlParamList", sqlParamList);
		else
			request.getSession().removeAttribute("sqlParamList");
		
		return mapping.findForward("sqlTemplate"); 
	}
	
	public ActionForward configParam(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		IdaTemplateForm idaform = (IdaTemplateForm)form;
		IdaTemplate template = idaform.getIdaTemplate();
		request.getSession().setAttribute("curTemplate", template);
		
		if (template.getTemplateTypeId().equals("10"))
			return mapping.findForward("configParam"); 
		else if (template.getTemplateTypeId().equals("30"))
			return mapping.findForward("configSqlParam"); 
		else
			return mapping.findForward("failure"); 
	}
	
	public IdaMgr getIdamgr(){
		return idamgr;
	}
	public void setIdamgr(IdaMgr p_idamgr){
		idamgr = p_idamgr;
	}
}